<?php
    /**
    *file-level documentation
    *@author Name <email@exp.com>
    *27 May 2012
    *@copyright Copyright (c) 2012, Tuncay Bakan
    *@package somePackage
    *
    */
    /**
    *the class Loader provides static methods to load and instanciate controllers, models and files
    *
    */
    class Loader{
        
        /**
         * loads the controller with the passed name 
         * 
         * @param String $sName The name of the controller that shall be instanciated
         * @return Object Controller returns an instance of a Controller with the passed name
         */
        public static function loadController($sName) {
            //the postfix of a controller class, every controller class has a name like 'ControllerName_Controller'
            $sIdentifier = '_Controller';
            
            $sControllerPath = WEBSITE_ROOT . 'controller/';
            $sSuffix = '.class.php';
            $sFileName = $sControllerPath . $sName . $sSuffix;
            $sClass = ucfirst($sName). $sIdentifier;
            
            //if the file does not exist or the the class is not defined an instance
            //of the default-controller is returned
            //else, an instance of the controller with the passed name is instanciated

            if(!(self::checkFile($sFileName)&&self::checkClass($sClass))){
                //recursive call to load the default controller
                return self::loadController(DEFAULT_CONTROLLER);
            } else {
                //return an instance of the constructor with the passed name

                return new $sClass;
            }
        }
        
    /**
         * loads the Model with the passed name 
         * 
         * @param String $sName The name of the model that shall be instanciated
         * @return Object Model returns an instance of a model with the passed name
         */
        public static function loadModel($sName) {
            //the postfix of a controller class, every controller class has a name like 'ControllerName_Controller'
            $sIdentifier = '_Model';
            
            $sModelPath = WEBSITE_ROOT . 'model/';
            $sSuffix = '.php';
            $sFileName = $sModelPath . $sName . $sSuffix;
            $sClass = ucfirst($sName). $sIdentifier;
            
            //if the file does not exist or the the class is not defined an instance
            //of the default-controller is returned
            //else, an instance of the controller with the passed name is instanciated

            if(!(self::checkFile($sFileName)&&self::checkClass($sClass))){
                //recursive call to load the default controller
                return self::loadModel(DEFAULT_MODEL);
            } else {
                //return an instance of the constructor with the passed name
                return new $sClass;
            }
        }
        
        public static function loadClass($sName){
            $sClassPath = WEBSITE_ROOT . 'lib/';
            $sSuffix = '.class.php';
            $sFileName = $sClassPath . strtolower($sName) . $sSuffix;
            $sClass = ucfirst($sName);
            
            
            if(!(self::checkFile($sFileName)&&self::checkClass($sClass))){
                //recursive call to load the default controller
                echo "The class does not exists";
            }
        }
        
        public static function loadFile($sFilePath){
            return self::checkFile($sFilePath);
        }
        
        /**
         * check if file exists
         * 
         * @param String $sFileName
         * @return boolean does the file exist?
         */
        private static function checkFile($sFilePath){
            if(file_exists($sFilePath)){
                //this line includes the passed file
                //and returns the return value of the include_once method
                //if include_once is successful it returns "1"                
                return include_once($sFilePath);
            } else {
                return false;
            }
        }
        
        /**
         * check if class exists.
         * can be called only after the file of the class was included before
         * 
         * @param String $sClass the name of the class
         * @return boolean does the class exist?
         */
        private static function checkClass($sClass){
            //only possible if checkFile() or include_once() was called before
            if(class_exists($sClass)){
                return true;
            }
            else {
                return false;
            }
        }
    }
?>
